﻿using CMWVS.Core.Mono;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CMWVS.Core.Commands
{
    public class RunCommand : RelayCommand<RunCommandParameters, RunCommandResult>
    {
        #region Constructor

        private RunCommand()
        {
        }

        internal RunCommand(Resolver resolver)
        {
            m_Resolver = resolver;
            this.SetCommands();
        }

        #endregion Constructor

        private Resolver m_Resolver;

        private void SetCommands()
        {
            base.m_CanExecute = () =>
            {
                MonoConfig config = m_Resolver.Get<MonoConfig>();
                return config.IsInstalled;
            };
            base.m_Commmand = (compileParameter) =>
            {
                MonoConfig config = m_Resolver.Get<MonoConfig>();

                foreach (RunCommandParameters.Item item in compileParameter.Items)
                {
                    Task t = Task.Factory.StartNew(() =>
                        {
                            Process p = new Process()
                            {
                                StartInfo = new ProcessStartInfo(config.MonoExe.FullName, item.ToRun + " " + item.Parameters)
                                {
                                    WorkingDirectory = item.WorkingDirectory.FullName
                                }
                            };
                            Trace.Assert(p.Start(), "Process did not start succesfully");
                        }, TaskCreationOptions.LongRunning);
                }
                return new RunCommandResult();
            };
        }
    }
}